﻿using System;
using System.Collections.Specialized;
using System.Web;

namespace Nydus.WebForms.FrontendModel
{
    /// <summary>
    /// Base class for all presenters.
    /// </summary>
    /// <typeparam name="TModel">The type of the model.</typeparam>
    public class BasePresenter<TModel, TView> 
                                where TView : IView<TModel> 
                                where TModel : new()
    {
        /// <summary>
        /// Gets or sets the model.
        /// </summary>
        public TModel Model { get; set; }

        /// <summary>
        /// Gets or sets the view.
        /// </summary>
        protected TView View { get; private set; }

        /// <summary>
        /// Gets or sets the http context.
        /// </summary>
        protected HttpContextBase CurrentHttpContext { get; private set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="BasePresenter&lt;TModel&gt;"/> class.
        /// </summary>
        /// <param name="view">The view.</param>
        /// <param name="modelPersistance">The model persistance store.</param>
        public BasePresenter(TView view, HttpContextBase httpContext)
        {
            this.View = view;
            this.CurrentHttpContext = httpContext;
            view.Init += new EventHandler(view_Init);
            view.Load += new EventHandler(view_Load);
            view.PreRender += new EventHandler(view_PreRender);
        }

        void view_Init(object sender, EventArgs e)
        {
            this.Model = new TModel();
            View.Model = this.Model;
        }

        void view_Load(object sender, EventArgs e)
        {
            OnLoad();
        }

        void view_PreRender(object sender, EventArgs e)
        {
            View.Model = this.Model;
        }

        /// <summary>
        /// Called when page is loaded and the model has been initialized.
        /// </summary>
        protected virtual void OnLoad()
        {
            
        }

        /// <summary>
        /// Redirects to the specified path.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <param name="arguments">The arguments.</param>
        protected void Redirect(string path, NameValueCollection arguments)
        {
            CurrentHttpContext.Response.Redirect(path+ToQueryString(arguments));
        }

        /// <summary>
        /// Gets the global resource.
        /// </summary>
        /// <param name="classKey">The class key.</param>
        /// <param name="resourceKey">The resource key.</param>
        /// <returns></returns>
        protected string GetGlobalResource(string classKey, string resourceKey)
        {
            return CurrentHttpContext.GetGlobalResourceObject(classKey, resourceKey) as string;
        }

        protected HttpSessionStateBase Session
        {
            get { return CurrentHttpContext.Session; }
        }

        /// <summary>
        /// Converts the name/value collection to query string.
        /// </summary>
        /// <param name="nvc">The name/value collection.</param>
        /// <returns></returns>
        private string ToQueryString(NameValueCollection nvc)
        {
            if (nvc == null)
                return String.Empty;

            return "?" + string.Join("&", 
                Array.ConvertAll(nvc.AllKeys, 
                key => string.Format("{0}={1}", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(nvc[key]))));
        }

        
    }
}
